public without sharing class CountTheLowerPriceHandler {
    ///当调价不是已批准的时候 新建或更新的时候

    //统计明细的低于成本价 二级经销商价 终端指导价 格的数量 并汇总到主表

    public static void apCnt  (List<AdjustPrice__c> adList,Map<Id,AdjustPrice__c> oldMap  ){

    	for (AdjustPrice__c ad  : adList) {
    		AdjustPrice__c old = oldMap.get(ad.Id );
    		if(ad.Status__c != '已批准' &&ad.Status__c != old.Status__c ){

    			apLowerCnt(ad,ad.Company__c );
    		}
    	}

    }

    public static void quoteCnt (List<Quote> qList,Map<Id,Quote> oldMap  ){
    	Map<Id,Quote> qMap = new Map<Id,Quote>();
    	Set<Id> oppset = new Set<Id>();
    	for (Quote q : qList) {
    		Quote old = oldMap.get(q.Id );
    		if(q.Status != '已批准'&&  q.Status != old.Status ){
    			qMap.put(q.Id , q);
    			oppset.add(q.OpportunityId);
    		}
    	}
    	if(oppset.isEmpty()) return;
    	Map<Id,String> oppMap  = new Map<Id,String>();
        Map<Id,String> oppProvince = new Map<Id,String> ();

    	for(Opportunity opp: [select Id, Owner.SubCompany__c ,Owner.PriceProvince__c  from Opportunity where Id in: oppset ]){
    		oppMap.put(opp.Id , opp.Owner.SubCompany__c);
            oppProvince.put(opp.Id , opp.Owner.PriceProvince__c );

    	}
    	for(Quote q : qMap.values()){
    		if(!oppMap.containsKey(q.OpportunityId))continue;
    		quoteLowerCnt(q,oppMap.get(q.OpportunityId),oppProvince.get(q.OpportunityId));
    	}

    	
    }
    //统计调价
    public static void apLowerCnt (AdjustPrice__c apm,String company  ){
        
        Map<String,HandlerBean > lowerMap = new Map<String,HandlerBean>();
        User u = [select Id ,PriceProvince__c from User where Id=:apm.OwnerId];

        List<AdjustPriceProduct__c> apList =  [select Id,Product__c,Product__r.Category1__c,AdjustSalesPrice__c,Product__r.GroupSubCompanyPrice__c from AdjustPriceProduct__c where AdjustPrice__c =: apm.Id];
        Set<Id> pset = new Set<Id>();
        for(AdjustPriceProduct__c ap :apList){
        	if(ap.Product__c != null )pset.add(ap.Product__c);
        }
        Map<Id,Price__c > priceMap = ImportObject.getPriceMap(pset,company,u.PriceProvince__c);

        for(AdjustPriceProduct__c ap :apList){

            if(ap.AdjustSalesPrice__c == null )continue;

        	String key = ap.Product__r.Category1__c+'_'+ apm.Id ;
        	HandlerBean hb = lowerMap.containsKey(key) ? lowerMap.get(key) : new HandlerBean();
        	hb.productline = ap.Product__r.Category1__c;
        	hb.mainId = apm.Id ;
        	Price__c pr = priceMap.get(ap.Product__c);
            if( ap.Product__r.GroupSubCompanyPrice__c != null && ap.AdjustSalesPrice__c < ap.Product__r.GroupSubCompanyPrice__c )hb.lowerThanGroupCnt++;
        	if(pr == null )continue;
        	if( pr.InstCost__c != null && ap.AdjustSalesPrice__c < pr.InstCost__c)hb.lowerThanCostCnt++;
        	if( pr.InvoiceDealerPrice__c != null && ap.AdjustSalesPrice__c < pr.InvoiceDealerPrice__c)hb.lowerThanInoviceCnt++;
        	if( pr.HospitalManagerPrice__c != null && ap.AdjustSalesPrice__c < pr.HospitalManagerPrice__c)hb.lowerThanHospitalCnt++;
        	if( pr.AreaDealerPrice__c != null && ap.AdjustSalesPrice__c < pr.AreaDealerPrice__c)hb.lowerThanAreaDealerCnt++;
        	if( pr.ProvincialAgencyPrice__c != null && ap.AdjustSalesPrice__c < pr.ProvincialAgencyPrice__c)hb.lowerThanAreaZDCnt++;
        	if( pr.SpecialPrice__c != null && ap.AdjustSalesPrice__c < pr.SpecialPrice__c)hb.lowerThanAreaZDCnt++;

        	lowerMap.put(key, hb);

        }
        
        //从价格体系获取价格
        if(lowerMap.isEmpty()) return;
       setLowerCnt(lowerMap.values(),apm);

    }
//统计报价
     //统计明细的低于成本价 二级经销商价 终端指导价 格的数量 并汇总到主表
    public static void quoteLowerCnt (Quote q,String company,String province ){
        
        Map<String,HandlerBean > lowerMap = new Map<String,HandlerBean>();

        List<QuoteProduct__c> apList =  [select Id,Product__c,Product__r.Category1__c,SalesPrice__c,Product__r.GroupSubCompanyPrice__c
         from QuoteProduct__c where Quote__c =: q.Id];

       
        Set<Id> pset = new Set<Id>();
        for(QuoteProduct__c ap :apList){
        	if(ap.Product__c != null )pset.add(ap.Product__c);
        }
         Map<Id,Price__c > priceMap = ImportObject.getPriceMap(pset,company ,province);

        for(QuoteProduct__c ap :apList){

            if(ap.SalesPrice__c == null )continue;

        	String key = ap.Product__r.Category1__c+'_'+ q.Id ;
        	HandlerBean hb = lowerMap.containsKey(key) ? lowerMap.get(key) : new HandlerBean();
        	hb.productline = ap.Product__r.Category1__c;
        	hb.mainId = q.Id;
        	Price__c pr = priceMap.get(ap.Product__c);
            if( ap.Product__r.GroupSubCompanyPrice__c != null && ap.SalesPrice__c < ap.Product__r.GroupSubCompanyPrice__c )hb.lowerThanGroupCnt++;
        	if(pr == null )continue;
        	if( pr.InstCost__c != null && ap.SalesPrice__c < pr.InstCost__c)hb.lowerThanCostCnt++;
        	if( pr.InvoiceDealerPrice__c != null && ap.SalesPrice__c < pr.InvoiceDealerPrice__c)hb.lowerThanInoviceCnt++;
        	if( pr.HospitalManagerPrice__c != null && ap.SalesPrice__c < pr.HospitalManagerPrice__c)hb.lowerThanHospitalCnt++;
        	if( pr.AreaDealerPrice__c != null && ap.SalesPrice__c < pr.AreaDealerPrice__c)hb.lowerThanAreaDealerCnt++;
        	if( pr.ProvincialAgencyPrice__c != null && ap.SalesPrice__c < pr.ProvincialAgencyPrice__c)hb.lowerThanAreaZDCnt++;
        	if( pr.SpecialPrice__c != null && ap.SalesPrice__c < pr.SpecialPrice__c)hb.lowerThanAreaZDCnt++;

        	lowerMap.put(key, hb);

        }
        
        //从价格体系获取价格
        
        if(lowerMap.isEmpty()) return;
       setLowerCnt(lowerMap.values(),q);

    }

    public static void setLowerCnt(List<HandlerBean> hbList,SObject sob  ){
    	 Map<String,SObject> upApMap = new Map<String,SObject>();
        for(HandlerBean hb : hbList){

        	//SObject ap = upApMap.containsKey(hb.mainId)? upApMap.get(hb.mainId) :Id.valueOf(hb.mainId).getSObjectType().newSObject();

        	//ap.Id = hb.mainId;
            sob.put('LowerthanGroupSubCompanyPrice__c',hb.lowerThanGroupCnt);
        	if(hb.productline == '病理'){
        		sob.put('LowerThanAreaDealderPriceBL__c', hb.lowerThanAreaDealerCnt);
        		sob.put('LowerThanAreaDealderPriceBLZD__c', hb.lowerThanAreaZDCnt);
        		sob.put('LowerThanInvoiceDealderPriceBL__c', hb.lowerThanInoviceCnt);
        		sob.put('LowerThanInstCostBL__c', hb.lowerThanCostCnt);
        		sob.put('LowerThanHospitalManagerPriceBL__c', hb.lowerThanHospitalCnt);
				
        	}else if(hb.productline == '免疫'){
        		sob.put('LowerThanAreaDealderPriceMY__c', hb.lowerThanAreaDealerCnt);
        		sob.put('LowerThanInvoiceDealderPriceMY__c', hb.lowerThanInoviceCnt);
        		sob.put('LowerThanInstCostMY__c', hb.lowerThanCostCnt);
        		sob.put('LowerThanHospitalManagerPriceMY__c', hb.lowerThanHospitalCnt);
        	}else if(hb.productline == '生化'){

        		sob.put('LowerThanAreaDealderPriceSH__c', hb.lowerThanAreaDealerCnt);
        		sob.put('LowerThanInvoiceDealderPriceSH__c', hb.lowerThanInoviceCnt);
        		sob.put('LowerThanInstCostSH__c', hb.lowerThanCostCnt);
        		sob.put('LowerThanHospitalManagerPriceXH__c', hb.lowerThanHospitalCnt);
        	}else if(hb.productline == 'POCT'){

        		sob.put('LowerThanAreaDealderPriceXT__c', hb.lowerThanAreaDealerCnt);
        		sob.put('LowerThanAreaDealderPriceXTTJ__c', hb.lowerThanSpecialCnt);
        		sob.put('LowerThanInvoiceDealderPriceXT__c', hb.lowerThanInoviceCnt);
        		sob.put('LowerThanInstCostXT__c', hb.lowerThanCostCnt);
        		sob.put('LowerThanHospitalManagerPriceXT__c', hb.lowerThanHospitalCnt);
        	}else if(hb.productline == '血型'){

        		sob.put('LowerThanAreaDealderPriceXX__c', hb.lowerThanAreaDealerCnt);
        		sob.put('LowerThanInvoiceDealderPriceXX__c', hb.lowerThanInoviceCnt);
        		sob.put('LowerThanInstCostXX__c', hb.lowerThanCostCnt);
        		sob.put('LowerThanHospitalManagerPriceXX__c', hb.lowerThanHospitalCnt);
        	}else if(hb.productline == '血液'){

        		sob.put('LowerThanAreaDealderPriceXY__c', hb.lowerThanAreaDealerCnt);
        		sob.put('LowerThanInvoiceDealderPriceXY__c', hb.lowerThanInoviceCnt);
        		sob.put('LowerThanInstCostXY__c', hb.lowerThanCostCnt);
        		sob.put('LowerThanHospitalManagerPriceXL__c', hb.lowerThanHospitalCnt);
        	}else if(hb.productline == '分子'){
                sob.put('LowerThanAreaDealderPriceFZ__c', hb.lowerThanAreaDealerCnt);
                sob.put('LowerThanInvoiceDealderPriceFZ__c', hb.lowerThanInoviceCnt);
                sob.put('LowerThanInstCostFZ__c', hb.lowerThanCostCnt);
                sob.put('LowerThanHospitalManagerPriceFZ__c', hb.lowerThanHospitalCnt);

             
            }else if(hb.productline == '糖化'){
                sob.put('LowerThanAreaDealderPriceTH__c', hb.lowerThanAreaDealerCnt);
                sob.put('LowerThanInvoiceDealderPriceTH__c', hb.lowerThanInoviceCnt);
                sob.put('LowerThanInstCostTH__c', hb.lowerThanCostCnt);
                sob.put('LowerThanHospitalManagerPriceTH__c', hb.lowerThanHospitalCnt);
                
             
            }else{
        		continue;
        	}
        	//upApMap.put(hb.mainId, ap);

        }
       // if(!upApMap.isEmpty())update upApMap.values();

    }


    public class HandlerBean{
    	public String productline ; //产品线
    	public String mainId ; //主表Id 
    	public Decimal lowerThanCostCnt =0 ; //低于成本价的个数
        public Decimal lowerThanGroupCnt =0 ; //低于集团子公司价的个数
    	public Decimal lowerThanInoviceCnt =0;//低于二级经销商价的个数 
    	public Decimal lowerThanHospitalCnt =0  ; //低于终端指导价的个数 
    	public Decimal lowerThanAreaDealerCnt = 0 ;//低于一级经销商价的个数
    	public Decimal lowerThanAreaZDCnt = 0 ; //低于省总代价的个数 
    	public Decimal lowerThanSpecialCnt = 0; //低于特级经销商价的个数
    
    }

}